package cse.om.chart;

import cse.om.object.SensitivityByDay;
import cse.om.vswn.Sensitivity;
import java.awt.Color;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class StackedBarChart extends JFrame {

	private static final long serialVersionUID = 1L;

	public StackedBarChart(String applicationTitle, String chartTitle, List<SensitivityByDay> list) {
		super(applicationTitle);
		// This will create the dataset
		CategoryDataset dataset = createDataset(list);
		// based on the dataset we create the chart
		JFreeChart chart = createChart(dataset, chartTitle);
		// we put the chart into a panel
		ChartPanel chartPanel = new ChartPanel(chart);
		// default size
		chartPanel.setPreferredSize(new java.awt.Dimension(600, 500));
		// add it to our application
		setContentPane(chartPanel);
	}

	/**
	 * Creates a sample dataset
	 */
	private CategoryDataset createDataset(List<SensitivityByDay> list) {
		DefaultCategoryDataset result = new DefaultCategoryDataset();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
		for (SensitivityByDay sByDay : list) {
			List<Sensitivity> sList = sByDay.getSensitivityList();
			String sDate = format.format(sByDay.getDate());
			for (Sensitivity s : sList) {
				try {
					result.incrementValue(1, s.getDescription(), sDate);
				} catch (org.jfree.data.UnknownKeyException e) {
					result.addValue(1, s.getDescription(), sDate);
				}
			}
		}
		return result;
	}

	/**
	 * Creates a chart
	 */
	private JFreeChart createChart(CategoryDataset dataset, String title) {

		JFreeChart chart = ChartFactory.createStackedBarChart(
			title, // chart title
			"Thời gian", // domain axis label
			"Tỉ lệ", // range axis label
			dataset, // data
			PlotOrientation.VERTICAL, // the plot orientation
			true, // legend
			true, // tooltips
			false // urls
		);

		CategoryPlot plot = (CategoryPlot) chart.getPlot();

		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
		rangeAxis.setNumberFormatOverride(NumberFormat.getNumberInstance());
		StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
		renderer.setRenderAsPercentages(true);
		renderer.setDrawBarOutline(false);
		renderer.setBaseItemLabelsVisible(true);
		renderer.setBaseItemLabelGenerator(
			new StandardCategoryItemLabelGenerator());

		chart.setBackgroundPaint(Color.white);
		plot.setBackgroundPaint(Color.white);
		plot.setRangeGridlinePaint(Color.blue);
		chart.setAntiAlias(true);

		return chart;

	}
}
